package com.jiantong.lawEnforcing.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.lawEnforcing.impl.dao.BorrowRecordDao;
import com.jiantong.lawEnforcing.model.BorrowRecord;
import com.jiantong.lawEnforcing.service.BorrowRecordService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 档案借阅记录
 * Created by mz03 on 2017/8/17.
 */
@Slf4j
@Service
@RpcProvider
public class BorrowRecordServiceImpl implements BorrowRecordService {

    private final BorrowRecordDao borrowRecordDao;

    @Autowired
    public BorrowRecordServiceImpl(BorrowRecordDao borrowRecordDao) {
        this.borrowRecordDao = borrowRecordDao;
    }


    /**
     * id 查找
     *
     * @param id
     * @return News
     */
    @Override
    public Response<BorrowRecord> finById(Long id) {
        try {
            BorrowRecord borrowRecord = borrowRecordDao.findById(id);
            if (borrowRecord != null) {
                return Response.ok(borrowRecord);
            } else {
                log.error("borrowRecord (id={}) is not found", id);
                return Response.fail("borrowRecord.not.found");
            }
        }catch (Exception e){
            log.error("failed to find borrowRecord(id={}),cause:{}",id, Throwables.getStackTraceAsString(e));
            return Response.fail("borrowRecord.find.fail");
        }
    }

    /**
     * 创建
     *
     * @param borrowRecord 单据对象
     * @return 主键 id
     */
    @Override
    public Response<Long> create(BorrowRecord borrowRecord) {
        try {
            borrowRecordDao.create(borrowRecord);
            return Response.ok(borrowRecord.getId());
        }catch (Exception e) {
            log.error("failed.to.create.borrowRecord:{}", Throwables.getStackTraceAsString(e));
            return Response.fail("create.borrowRecord.failed");
        }
    }

    /**
     * 更新
     *
     * @param borrowRecord 单据对象
     * @return true or false
     */
    @Override
    public Response<Boolean> update(BorrowRecord borrowRecord) {
        try{
            return Response.ok(borrowRecordDao.update(borrowRecord));
        }catch (Exception e){
            log.error("eeror :{}",Throwables.getStackTraceAsString(e));
            return Response.fail("update.failed");
        }
    }

    /**
     * 根据状态查询当前登录人可以查看文书的案件id列表或者是不可以查看文书的案件id列表
     *
     * @param map
     * @return
     */
    @Override
    public Response<List<Long>> findByStatus(Map<String, Object> map) {
        try {
            List<BorrowRecord> list = borrowRecordDao.list(map);
            List<Long> listId = new ArrayList<>();
            Date date = new Date();
            for (BorrowRecord borrowRecord : list) {

                if (borrowRecord.getCases_id()!=null) {
                    if (borrowRecord.getStatus()==2 && borrowRecord.getBorrow_end_date()!=null && borrowRecord.getBorrow_end_date().getTime()<=date.getTime()){
                        continue;
                    }
                    listId.add(borrowRecord.getCases_id());
                }
            }
            return Response.ok(listId);
        } catch (Exception e) {
            log.error("查询当前可查看文书案件的id列表出错,error :{}",Throwables.getStackTraceAsString(e));
            return Response.fail("查询当前可查看文书案件的id列表出错");
        }
    }

    /**
     * 根据条件分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param map
     * @return
     */
    @Override
    public Response<Paging<BorrowRecord>> paging(Integer pageNo, Integer pageSize, Map<String, Object> map) {
        try{
            PageInfo pageInfo = new PageInfo(pageNo,pageSize);
            return Response.ok(borrowRecordDao.paging(pageInfo.getOffset(),pageInfo.getLimit(),map));
        }catch (Exception e){
            log.error("failed to find BorrowRecord list cause:{}",  Throwables.getStackTraceAsString(e));
            return Response.fail("BorrowRecord.list.find.fail");
        }
    }
}
